package cn.itsource.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.itsource.core.SmsTemplate;
import cn.itsource.domain.Driver;
import cn.itsource.domain.DriverAuthMaterial;
import cn.itsource.mapper.DriverAuthMaterialMapper;
import cn.itsource.mapper.DriverMapper;
import cn.itsource.pojo.driverenum.DriverAuthStatusEnum;
import cn.itsource.pojo.ro.GetDriverStatusRo;
import cn.itsource.pojo.ro.SaveDriverAuthMaterialRo;
import cn.itsource.service.IDriverAuthMaterialService;
import cn.itsource.utils.StrUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 司机实名资料 服务实现类
 * </p>
 *
 * @author zls
 * @since 2024-07-02
 */
@Service
@Transactional
public class DriverAuthMaterialServiceImpl extends ServiceImpl<DriverAuthMaterialMapper, DriverAuthMaterial> implements IDriverAuthMaterialService {
    @Autowired
    private DriverMapper driverMapper;

    @Autowired
    private SmsTemplate smsTemplate;
    /**
     * 审批认证
     * @param driverStatusRo
     * @return
     */
    @Override
    public void audit(GetDriverStatusRo driverStatusRo) {
        DriverAuthMaterial driverAuthMaterial = super.getById(driverStatusRo.getId());
        if (driverStatusRo.getApprove()){
            driverAuthMaterial.setRealAuthStatus(DriverAuthStatusEnum.DRIVER_AUTH_SUCCESS.getCode());
            driverAuthMaterial.setAuditTime(new Date());
            driverAuthMaterial.setDriverId(driverStatusRo.getId());
            driverAuthMaterial.setAuditRemark(driverStatusRo.getRemark());
            super.updateById(driverAuthMaterial);
            //审核完成发送短信
            //smsTemplate.sendSms(driverAuthMaterial.getPhone(),"你现在可以开启您的打工之旅");
        }else {
            driverAuthMaterial.setRealAuthStatus(DriverAuthStatusEnum.DRIVER_AUTH_FAIL.getCode());
            driverAuthMaterial.setAuditTime(new Date());
            driverAuthMaterial.setDriverId(driverStatusRo.getId());
            driverAuthMaterial.setAuditRemark(driverStatusRo.getRemark());
            super.updateById(driverAuthMaterial);
            //审核完成发送短信
            //smsTemplate.sendSms(driverAuthMaterial.getPhone(),StrUtils.getRandomString(6));
        }
    }

    /**
     * 撤销审批认证
     * @param driverStatusRo
     */
    @Override
    public void cancel(GetDriverStatusRo driverStatusRo) {
        DriverAuthMaterial driverAuthMaterial = super.getById(driverStatusRo.getId());
        if (driverStatusRo.getApprove()){
            driverAuthMaterial.setRealAuthStatus(DriverAuthStatusEnum.DRIVER_NO_AUTH.getCode());
            driverAuthMaterial.setAuditTime(new Date());
            driverAuthMaterial.setDriverId(driverStatusRo.getId());
            driverAuthMaterial.setAuditRemark(driverStatusRo.getRemark());
            super.updateById(driverAuthMaterial);
        }
    }

    @Override
    public Boolean getDriverStatus() {
        String openId = (String) StpUtil.getExtra("openId");
        Driver driver = driverMapper.selectOne(new LambdaQueryWrapper<Driver>().eq(Driver::getOpenId, openId));
        DriverAuthMaterial driverAuthMaterial = super.getById(driver.getId());
        return driverAuthMaterial.getRealAuthStatus()==1;
    }

    @Override
    public void save(SaveDriverAuthMaterialRo driverAuthMaterialRo) {
        DriverAuthMaterial driverAuthMaterial = SaveDriverAuthMaterialRo.toDriverAuthMaterial(driverAuthMaterialRo);
        //实名认证 百度的 todo
        //https://cloud.baidu.com/doc/FACE/s/elav5puig
        //手动设置审核状态
        driverAuthMaterial.setRealAuthStatus(DriverAuthStatusEnum.DRIVER_NO_AUTH.getCode());
        super.save(driverAuthMaterial);
    }

}
